package com.shuai.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.RemoteViews;

import com.shuai.adapter.PlantListService;
import com.shuai.sql.SQL;
import com.shuai.stickynote.EditActivity;
import com.shuai.stickynote.R;
import com.shuai.stickynote.TextNote;
import com.shuai.tool.ClassFactory;
import com.shuai.tool.MyTime;
import com.shuai.tool.SendTool;

import java.util.HashMap;
import java.util.Map;

public class TextWidget extends AppWidgetProvider {
    private static Map<Integer, RemoteViews> viewsMap;
    private static Map<Integer,Integer> widgetIds;
    private static String type = "";
    private static String TAG = "fuck";

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String action = intent.getAction();
        if (action.equals(SendTool.WIDGET_ACTION_TEXT) || action.equals(SendTool.WIDGET_ACTION_PLANT)
            || action.equals(SendTool.WIDGET_ACTION_TASK) || action.equals(SendTool.WIDGET_ACTION_MENORY)){
            int id = intent.getIntExtra("id",0);
            String noteId = intent.getStringExtra(SendTool.NOTE_ID);
            type = intent.getStringExtra(SendTool.TYPE);
            RemoteViews views = null;
            switch (type){
                case SendTool.TYPE_TEXT:
                    views = setTypeText(context,noteId,id);
                    break;
                case SendTool.TYPE_PLANT:
                    views = setTypePlant(context,noteId,id);
                    break;
            }
            Log.d(TAG, "onReceive: "+(views == null));
            AppWidgetManager.getInstance(context).updateAppWidget(id,views);
        }
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
        SharedPreferences sp = context.getSharedPreferences("info",Context.MODE_PRIVATE);
        String noteId = sp.getString(appWidgetId+SendTool.NOTE_ID,"");
        type = sp.getString(appWidgetId+SendTool.TYPE,"");
        RemoteViews views = null;
        switch (type){
            case SendTool.TYPE_TEXT:
                views = updateWidgetForText(context,noteId,appWidgetId);
                openACT(context,views,appWidgetId,R.id.text_widget_click);
                break;
            case SendTool.TYPE_PLANT:
                views = updateWidgetForPlant(context,noteId,appWidgetId);
                openACT(context,views,appWidgetId,R.id.plant_widget_click);
                break;
            default:
                views = new RemoteViews(context.getPackageName(),R.layout.default_widget);
                openACT(context,views,appWidgetId,R.id.default_widget_click);
                break;
        }
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        if (viewsMap == null) viewsMap = new HashMap<>();
        if (widgetIds == null) widgetIds = new HashMap<>();
        for (int appWidgetId : appWidgetIds) {
            widgetIds.put(appWidgetId,appWidgetId);
            RemoteViews views = SendTool.RemoteViewsFactory.getRemoteViews(context,type);
            viewsMap.put(appWidgetId,views);
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }
//    设置点击事件
    private static void openACT(Context context, RemoteViews view, int id, int clickViewId){
        Intent intent = new Intent(context,EditActivity.class);
        intent.setAction(SendTool.WIDGET_ACTION_TEXT);
        intent.putExtra("id", id);
        intent.putExtra(ClassFactory.SIZE,ClassFactory.SIZE_2X2);
        PendingIntent pi = PendingIntent.getActivity(context,id,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        view.setOnClickPendingIntent(clickViewId,pi);
    }
//    获取小部件的id
    public static Map<Integer, Integer> getWidgetIds() {
        return widgetIds;
    }
//    onReceive处设置小部件显示的内容
    private static RemoteViews setTypeText(Context context, String noteId, int id){
        SharedPreferences sp = context.getSharedPreferences("info",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        RemoteViews views = null;
        TextNote data = null;
        SQL sql = new SQL(context);
        views = SendTool.RemoteViewsFactory.getRemoteViews(context,SendTool.TYPE_TEXT);
        data = sql.selectTextById(noteId);
        views.setTextViewText(R.id.text_widget_title,data.getTitle());
        views.setTextViewText(R.id.text_widget_content,data.getContent());
        openACT(context,views,id,R.id.text_widget_click);
        editor.putString(id+SendTool.NOTE_ID,data.getId());
        editor.putString(id+SendTool.TYPE,SendTool.TYPE_TEXT);
        editor.commit();
        return views;
    }
    private static RemoteViews setTypePlant(Context context, String noteId, int id){
        SharedPreferences sp = context.getSharedPreferences("info",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        RemoteViews views = SendTool.RemoteViewsFactory.getRemoteViews(context,type);
        String day = MyTime.toDateString(noteId);
//        views.setTextViewText(R.id.widget_plant_title,day);
//        Intent intent = new Intent(context, PlantListService.class);
//        intent.putExtra("day",noteId);
//        views.setRemoteAdapter(R.id.widget_plant_list_2,intent);
//        openACT(context,views,id,R.id.widget_plant_title);
        editor.putString(id+SendTool.NOTE_ID,noteId);
        editor.putString(id+SendTool.TYPE,SendTool.TYPE_PLANT);
        editor.commit();
        return views;
    }
//    updateAppWidget处设置小部件显示的内容
    private static RemoteViews updateWidgetForText(Context context, String noteId, int widgetId){
        CharSequence widgetText = "";
        CharSequence widgetTitle = "";
        TextNote data = null;
        RemoteViews views = null;
        if (noteId.equals("")){
            widgetText = context.getString(R.string.appwidget_text);
            widgetTitle = context.getString(R.string.appwidget_title);
        }else {
            SQL sql = new SQL(context);
            data = sql.selectTextById(noteId);
            widgetText = data.getContent();
            widgetTitle = data.getTitle();
            widgetIds.put(widgetId,widgetId);
        }
        if (viewsMap != null) views = viewsMap.get(widgetId);
        else views = SendTool.RemoteViewsFactory.getRemoteViews(context,type);
        views.setTextViewText(R.id.text_widget_title,widgetTitle);
        views.setTextViewText(R.id.text_widget_content, widgetText);
        openACT(context,views,widgetId,R.id.text_widget_click);
        return views;
    }
    private static RemoteViews updateWidgetForPlant(Context context, String noteId, int widgetId){
        RemoteViews views = null;
        if (viewsMap != null) views = viewsMap.get(widgetId);
        else views = SendTool.RemoteViewsFactory.getRemoteViews(context,type);
        if (!noteId.equals("")){
            String day = MyTime.toDateString(noteId);
            views.setTextViewText(R.id.widget_plant_day,day);
            Intent intent = new Intent(context, PlantListService.class);
            intent.putExtra("day",noteId);
            views.setRemoteAdapter(R.id.widget_plant_list_2,intent);
        }
        openACT(context,views,widgetId,R.id.text_widget_click);
        return views;
    }

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        SharedPreferences sp = context.getSharedPreferences("info",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        for (int id : appWidgetIds) {
            editor.remove(id+SendTool.NOTE_ID);
            editor.remove(id+SendTool.TYPE);
            editor.commit();
        }
    }
}
